package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.server.CourseServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseServer courseServer;

    //查询列表
    @GetMapping
    public Result findCourseByList(String name, Integer subject, Integer target,
                                   @RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer pageSize){
        PageResult<Course> result =courseServer.findCourseByList(name,subject,target,page,pageSize);
        log.info("查询列表成功:{}",result);
        return Result.success(result);
    }
    //删除列表
    @Log
    @DeleteMapping("/{id}")
    public Result delCourseById(@PathVariable Integer id){
        courseServer.deleteById(id);
        log.info("删除列表成功");
        return Result.success();
    }
    //添加数据
    @Log
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        courseServer.addCourse(course);
        log.info("添加数据成功");
        return Result.success();
    }
    //根据Id查询数据
    @GetMapping("/{id}")
    public Result findCourseById(@PathVariable Integer id){
        Course course = courseServer.findCourseById(id);
        log.info("id:{}为:{}",id,course);
        return Result.success(course);
    }
    //修改数据
    @Log
    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        courseServer.updateCourse(course);
        log.info("修改数据成功");
        return Result.success();
    }
    //查询所有数据
    @GetMapping("/list")
    public Result findAll(){
        List<Course> courses = courseServer.findAll();
        log.info("查询所有数据成功:{}",courses);
        return Result.success(courses);
    }
    //根据学科查询数据
    @GetMapping("/subject/{subject}")
    public Result findCourseBySubject(@PathVariable Integer subject){
        List<Course> courses = courseServer.findCourseBySubject(subject);
        log.info("查询所有数据成功:{}",courses);
        return Result.success(courses);
    }
}
